#!coding:utf-8
# vim: tabstop=4 shiftwidth=4 softtabstop=4
from lxml import etree
import logging

from cvirt.cnode.libvirtXmlObject.baseDomain import GuestDevice

LOG = logging.getLogger(__name__)

"""
    @author jay.han
"""

class BaseCharDevice(GuestDevice):
    ROOT_NAME = "channel"
    def __init__(self, parse_xml=None, **kw):
        super(BaseCharDevice, self).__init__(parse_xml=parse_xml,**kw)

        self.type = "pty"
        self.source_path = None

    def _get_xml_config(self, root):
        dev = root
        dev.set("type", self.type)
        if self.type == "file":
            dev.append(etree.Element("source", path=self.source_path))

        return dev

class GuestCharDevice(BaseCharDevice):

    def __init__(self, parse_xml=None, **kw):
        super(BaseCharDevice, self).__init__(parse_xml=parse_xml,**kw)
        self.target_port = None

    def _get_xml_config(self, root):
        dev = root

        if self.target_port is not None:
            dev.append(etree.Element("target", port=str(self.target_port)))

        return dev

class GuestSerial(BaseCharDevice):

    def __init__(self, parse_xml=None, **kw):
        super(GuestSerial, self).__init__(parse_xml=parse_xml,**kw)


class GuestConsole(BaseCharDevice):

    def __init__(self, parse_xml=None, **kw):
        super(GuestConsole, self).__init__(parse_xml=parse_xml,**kw)


class Channel(GuestDevice):
    ROOT_NAME = "channel"
    def __init__(self, parse_xml=None, **kw):
        super(Channel, self).__init__(parse_xml=parse_xml,**kw)
        self.type = 'spicevmc'
        self.target_type = "virtio"
        self.target_name = None
        self.source_mode = None
        self.source_path = None

    def _parse_xml(self, xmldoc):
        self.type = xmldoc.get('type')
        for c in xmldoc.getchildren():
            if c.tag == 'target':
                self.target_type = c.get('type')
                self.target_name = c.get('name')
            if c.tag == 'source':
                self.source_path = c.get('path')
                self.source_mode = c.get('mode')

    def _get_xml_config(self, root):
        dev = root
        dev.set('type',self.type)

        if self.target_name:
            target = etree.Element("target", type=self.target_type)
            target.set("name", self.target_name)
            dev.append(target)
        if self.source_mode:
            source = etree.Element('source',mode=self.source_mode,path=self.source_path)
            dev.append(source)

        return dev

class UnixChannel(Channel):
    """
        root channel
        <channel type='unix'>
            <source mode='bind' path='${guest_agent_path}'/>
            <target type='virtio' name='com.morningcloud.tcloud'/>
        </channel>
    """
    def __init__(self, parse_xml=None, **kw):
        super(UnixChannel, self).__init__(parse_xml=parse_xml,**kw)
        self.type = 'unix'
        self.target_type = "virtio"
        self.target_name = 'com.morningcloud.tcloud'
        self.source_mode = 'bind'
        self.source_path = None

